﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.COM;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class M009_StocktakingList : FormBase
    {
        public M009_StocktakingList()
        {
            InitializeComponent();
        }

        #region 变量定义

        BLL.BLL_M014BILLSCAN bll_M014BILLSCAN = new XiaoGang.Soft.XGWMS.BLL.BLL_M014BILLSCAN();
        string strGroupTitle = "盘库单@@@   提示：双击明细可查看该盘库单的详细信息";
        private const string dataName = "盘库";
        
        #endregion

        #region 方法

        /// <summary>	
        /// <p><b>方法名称</b>SetTabIndex</p>	
        /// <p><b>概要説明	:</b>画面フォーカスの遷移順の設定</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/10</p>	
        /// </summary>
        private void SetTabIndex()
        {
            int intIndex = 0;
            btnNewStocktaking.TabIndex = intIndex; intIndex++;
            btnLastStocktaking.TabIndex = intIndex; intIndex++;
            btnRefresh.TabIndex = intIndex; intIndex++;
            btnClose.TabIndex = intIndex; intIndex++;
        }

        /// <summary>	
        /// <p><b>方法名称</b>ClearControl</p>	
        /// <p><b>概要説明	:</b>画面上の全項目をオールクリアする</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/10</p>	
        /// </summary>
        private void ClearControl()
        {
        }

        /// <summary>	
        /// <p><b>方法名称</b>SetEnable</p>	
        /// <p><b>概要説明  :</b>画面モードのセト</p>
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/10</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        private void SetEnable()
        {
            btnNewStocktaking.Enabled = true;
            btnLastStocktaking.Enabled = true;
            btnRefresh.Enabled = true;
            btnClose.Enabled = true;
        }

        /// <summary>	
        /// <p><b>方法名称</b>InitialGridView</p>	
        /// <p><b>概要説明	:</b>窗口初始化</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/10</p>	
        /// </summary>
        private void InitialGridView()
        {
            grdmentBillInfo.ReadOnly = true;
            grdmentBillInfo.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            grdmentBillInfo.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            grdmentBillInfo.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            grdmentBillInfo.ColumnHeadersHeight = 30;
            grdmentBillInfo.ColumnHeadersVisible = true;
            grdmentBillInfo.AllowUserToResizeColumns = true;
            grdmentBillInfo.ColumnHeadersDefaultCellStyle.Alignment =
                DataGridViewContentAlignment.MiddleCenter;

            grdmentBillInfo.Columns.Add("NO.", "NO.");
            grdmentBillInfo.Columns.Add("BILL_ID", "盘库编号");
            grdmentBillInfo.Columns.Add("BUSINESS_TIME", "盘库时间");
            grdmentBillInfo.Columns.Add("Stocktakinger", "盘库人");
            grdmentBillInfo.Columns.Add("STOREROOM_NM", "所盘仓库");
            grdmentBillInfo.Columns.Add("STOREROOM_ID", "");

            grdmentBillInfo.Columns["NO."].Width = 50;
            grdmentBillInfo.Columns["NO."].Frozen = true;
            grdmentBillInfo.Columns["NO."].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentBillInfo.Columns["NO."].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentBillInfo.Columns["NO."].Resizable = DataGridViewTriState.False;
            grdmentBillInfo.Columns["BILL_ID"].Width = 200;
            grdmentBillInfo.Columns["BILL_ID"].Frozen = true;
            grdmentBillInfo.Columns["BILL_ID"].Resizable = DataGridViewTriState.False;
            grdmentBillInfo.Columns["BILL_ID"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentBillInfo.Columns["BILL_ID"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentBillInfo.Columns["BUSINESS_TIME"].Width = 227;
            grdmentBillInfo.Columns["BUSINESS_TIME"].Resizable = DataGridViewTriState.False;
            grdmentBillInfo.Columns["BUSINESS_TIME"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentBillInfo.Columns["BUSINESS_TIME"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdmentBillInfo.Columns["Stocktakinger"].Width = 306;
            grdmentBillInfo.Columns["Stocktakinger"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentBillInfo.Columns["Stocktakinger"].Resizable = DataGridViewTriState.False;
            grdmentBillInfo.Columns["STOREROOM_NM"].Width = 300;
            grdmentBillInfo.Columns["STOREROOM_NM"].SortMode = DataGridViewColumnSortMode.NotSortable;
            grdmentBillInfo.Columns["STOREROOM_NM"].Resizable = DataGridViewTriState.False;
            grdmentBillInfo.Columns["STOREROOM_ID"].Width = 0;
            grdmentBillInfo.Columns["STOREROOM_ID"].Visible = false;

            grdmentBillInfo.AllowUserToAddRows = false;
            grdmentBillInfo.AllowUserToDeleteRows = false;
            grdmentBillInfo.AllowUserToResizeRows = false;
            grdmentBillInfo.RowHeadersVisible = false;
            grdmentBillInfo.RowTemplate.Height = 20;
            grdmentBillInfo.RowTemplate.ReadOnly = true;
            grdmentBillInfo.RowTemplate.Resizable = DataGridViewTriState.False;
            grdmentBillInfo.Height = 362;
        }        

        /// <summary>	
        /// <p><b>方法名称</b>InitProcess</p>	
        /// <p><b>概要説明	:</b>窗口初始化</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/10</p>	
        /// </summary>
        private void InitProcess()
        {
            dtpDateFrom.Text = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
            dtpDateTo.Text = DateTime.Now.ToString("yyyy-MM-dd");
            SetTabIndex();
            SetEnable();
            InitialGridView();
            ClearControl();
            ShowGridData();
        }

        /// <summary>	
        /// <p><b>方法名称</b>ShowGrid</p>	
        /// <p><b>概要説明  :</b>検索結果をGridに表示する</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/08</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="class" param="model">库存信息表</param>	
        private void ShowGrid(MODEL.MOD_M010_BILL model)
        {
            grdmentBillInfo.Rows.Clear();
            ReturnValue rvReturnValue = bll_M014BILLSCAN.SearchProcess(model);
            if (rvReturnValue.IsSuccess == false)
            {
                Method.MessageShow(rvReturnValue.ErrorID,dataName);
                return;
            }
            if (0 == rvReturnValue.ResultDataSet.Tables[0].Rows.Count)
            {                
                grdmentBillInfo.Rows.Clear();
                grpSearchResult.Text = strGroupTitle.Replace("@@@", "");
                return;
            }
            foreach (DataRow row in rvReturnValue.ResultDataSet.Tables[0].Rows)
            {
                grdmentBillInfo.Rows.Add(
                    (grdmentBillInfo.Rows.Count + 1).ToString(),
                    row["BILL_ID"].ToString(),
                    DateFormat(row["BUSINESS_TIME"].ToString().Replace("/","")),
                    row["USER_NAME"].ToString(),
                    row["STOREROOM_NM"].ToString(),
                    row["STOREROOM_ID"].ToString());
            }
            grpSearchResult.Text = strGroupTitle.Replace("@@@","  共" + rvReturnValue.ResultDataSet.Tables[0].Rows.Count.ToString() + "条");
            if (grdmentBillInfo.Rows.Count > 17)
            {
                grdmentBillInfo.Columns["BUSINESS_TIME"].Width = 210;
            }
            else
            {
                grdmentBillInfo.Columns["BUSINESS_TIME"].Width = 227;
            }
        }

        /// <summary>	
        /// <p><b>方法名称</b>DateFormat</p>	
        /// <p><b>概要説明  :</b>date data format</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/18</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <param name="strValue">没有格式化的字符串</param> 
        /// <returns type="string">格式化后的字符串</returns>
        protected string DateFormat(string strValue)
        {
            if (strValue.Length < 8)
            {
                return strValue;
            }
            return strValue.Substring(0, 4) + "/" +
                strValue.Substring(4, 2) + "/" +
                strValue.Substring(6, 2);
        }

        private void ShowGridData()
        {
            MODEL.MOD_M010_BILL model = new XiaoGang.Soft.XGWMS.MODEL.MOD_M010_BILL();
            model.IN_OUT = "2";//盘库
            model.BUSINESS_START_TIME = dtpDateFrom.Text.Trim() == "" ? dtpDateFrom.Value.ToString("yyyyMMdd") : dtpDateFrom.Text.Trim().Replace("-", "").Replace("/", "");
            model.BUSINESS_END_TIME = dtpDateTo.Text.Trim() == "" ? dtpDateTo.Value.ToString("yyyyMMdd") : dtpDateTo.Text.Trim().Replace("-", "").Replace("/", "");
            ShowGrid(model);
        }

        #endregion 

        #region 事件

        private void grdmentBillInfo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (Cursor.Current == Cursors.WaitCursor)
            {
                return;
            }
            if (0 == grdmentBillInfo.Rows.Count || e.RowIndex == -1)
            {
                return;
            }
            GlobalVal.SplashObj = SplashObject.GetLoading();
            string strBillID = grdmentBillInfo.CurrentRow.Cells["BILL_ID"].Value.ToString();
            M009_Stocktaking frm_M009_Stocktaking = new M009_Stocktaking(strBillID);
            frm_M009_Stocktaking.storageRoomID = grdmentBillInfo.CurrentRow.Cells["STOREROOM_ID"].Value.ToString();
            frm_M009_Stocktaking.StartPosition = FormStartPosition.CenterScreen;
            frm_M009_Stocktaking.ShowDialog();
        }

        protected override void AllTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            base.AllTextBox_KeyDown(sender, e);
        }

        private void AllButton_Click(object sender, EventArgs e)
        {
            if (Cursor.Current == Cursors.WaitCursor)
            {
                return;
            }
            Cursor.Current = Cursors.WaitCursor;

            string strButtonName = ((Button)sender).Name;

            switch (strButtonName)
            {
                #region 新盘库
                case "btnNewStocktaking":
                    {
                        try
                        {
                            if (GlobalVal.gloformStoreRoomSelect == null)
                            {
                                GlobalVal.gloformStoreRoomSelect = new FormStoreRoomSelect();
                            }
                            GlobalVal.gloformStoreRoomSelect.ShowDialog();
                        }
                        catch { }
                    }
                    break;
                #endregion

                #region 上次盘库
                case "btnLastStocktaking":
                    {
                        ReturnValue returnValue = bll_M014BILLSCAN.GetLastStocktakingID();
                        if (!returnValue.IsSuccess)
                        {
                            Method.MessageShow(returnValue.ErrorID,dataName);
                            return;
                        }
                        if (returnValue.ResultDataSet.Tables[0].Rows.Count < 1)
                        {
                            Method.MessageShow("W11048", dataName);//目前还没有盘库记录，请点击新盘库进行盘库！
                            return;
                        }
                        string strBillID = returnValue.ResultDataSet.Tables[0].Rows[0]["BILL_ID"].ToString();
                        M009_Stocktaking frm_M009_Stocktaking = new M009_Stocktaking(strBillID);
                        frm_M009_Stocktaking.storageRoomID = returnValue.ResultDataSet.Tables[0].Rows[0]["STOREROOM_ID"].ToString();
                        frm_M009_Stocktaking.StartPosition = FormStartPosition.CenterScreen;
                        frm_M009_Stocktaking.ShowDialog();
                    }
                    break;
                    #endregion

                #region 刷新
                case "btnRefresh":
                    {
                        ShowGridData();
                    }
                    break;
                #endregion

                #region 退出
                case "btnClose":
                    {
                        if (this.Cursor == Cursors.WaitCursor)
                        {
                            return;
                        }
                        this.Close();
                    }
                    break;
                    #endregion    
            }
            Cursor.Current = Cursors.Default;
        }

        

        private void M009_StocktakingList_Load(object sender, EventArgs e)
        {
            InitProcess();
            btnNewStocktaking.Focus();
        }

        private void M009_StocktakingList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    if (this.Cursor == Cursors.WaitCursor)
                    {
                        return;
                    }
                    this.Close();
                    break;
                case Keys.F5:
                    AllButton_Click(btnRefresh, e);
                    break;
            }
        }

        //private void M009_StocktakingList_Activated(object sender, EventArgs e)
        //{
        //    if (GlobalVal.globFinishStocktaking)
        //    {
        //        ShowGridData();
        //        GlobalVal.globFinishStocktaking = false;
        //    }
        //}

        #endregion
    }
}